var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    id: '',
    mainHeight:'100vh',
    delivery_date: ''//util.dateObjectformat(new Date(),'yyyy-MM-dd')
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--运单设置" });
    that.setData({
      id: e.id
    });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          mainHeight: res.windowHeight - 55 + 'px'
        });
      }
    });
    //极速达|1|0,便利购|1|0,保温箱|1|0,包材/其它|1|0,纸箱|0.25|0
    util.getData("deliveryOnly", {
      id: e.id
    }, function (res, error) {
      console.log(res.data)
      var data = res.data.data[0];
      var shop = res.data.shop;
      that.setData(data);
      if (data.loading_rule_wh) {
        var loading_rule_wh = data.loading_rule_wh;
        if (loading_rule_wh.indexOf("shop|") > -1 && shop.length > 0){
          var _loading_rule_wh = new Array();
           for(var i=0;i<shop.length;i++){
             _loading_rule_wh[i] = shop[i].name + '_' + shop[i].shop + '|1|0';
           }
          loading_rule_wh = _loading_rule_wh.join(",");
        }
        if (data.loading_rule == 'undefined' || data.loading_rule == 'null'){
          data.loading_rule = null;
        }
        var loadingRule = [], loading_rule = String(data.loading_rule || loading_rule_wh).split(",");
        for (var i in loading_rule) {
          var _item = String(loading_rule[i]).split("|");
          loadingRule.push({ "id": "LDR" + i, "name": _item[0], "ratio": _item[1], "number": _item[2] });
        }
        that.setData({
          loadingRule: loadingRule
        });
      }
    });
  },
  onReady: function () {
    wx.hideToast()
  },
  bindfocus:function(e){
    var value = String(e.detail.value).length;
    
  },
  bindInput: function (e) {
    var that = this;
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
    if (item.indexOf("LDR")>-1){
      var loadingRule = that.data.loadingRule;
      for (var i = 0; i < loadingRule.length;i++){
        if (loadingRule[i].id == item){
          loadingRule[i].number = Number(e.detail.value);
          break;
        }
      }
      that.setData({
        loadingRule: loadingRule
      });
      that.calculateTotalLoading();
    }
  },
  calculateTotalLoading: function () {
    var that = this;  
    var loadingRule = that.data.loadingRule, _loading_rule = new Array(), packages_expect = 0;
    for (var i = 0; i < loadingRule.length; i++) {
      packages_expect += Number(loadingRule[i].number) * Number(loadingRule[i].ratio);
      _loading_rule[i] = loadingRule[i].name + "|" + loadingRule[i].ratio + "|" + loadingRule[i].number;
    }
    that.setData({
      loading_rule: _loading_rule.join(","),
      packages_expect: packages_expect.toFixed(2)
    })
  },
  switchChange:function(e){
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value?1:0;
    this.setData(item_value);
  },
  confirmData: function () {
    var that = this;
    // if (!/^-?[0-9]\d*$/.test(that.data.specialPrice)) {
    //   wx.showToast({
    //     title: '特殊补贴错误',
    //     image: '/images/wrong.png',
    //     duration: 3000
    //   });
    //   that.setData({ "specialPrice": 0});
    //   return false;
    // }
    let target = {
      returnBoxs: that.data.returnBoxs?1:0,
      deliveryOntime: that.data.deliveryOntime||'',
      deliveryEvaluation: that.data.deliveryEvaluation||'',
      urgentGetVehicle: that.data.urgentGetVehicle||'',
      remoteGetVehicle: that.data.remoteGetVehicle||'',
      specialPrice: that.data.specialPrice||'',
      configExplain: that.data.configExplain||'',
      loading_rule: that.data.loading_rule||''
    }
    if(that.data.total_mileage){
      target.total_mileage = that.data.total_mileage
    }
    if(that.data.back_distance){
      target.back_distance = that.data.back_distance
    }
    util.postData("/sys/form/commonUpdate",{
      "formId":"6E6999E1-1A6B-4F98-B1A4-A0A6BD77FAFC",
      "data":[{
          "dataId":that.data.id,
          "target":target
      }],
      "condition":""
      }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        return false;
      }
      if (res.data.success) {
        wx.showToast({
          title: '设置成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/schedule/schedule'
              });
            }, 2000);
          }
        })
      }else{
        wx.showToast({
          title: "设置失败",
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  cancelConfirmData: function () {
    wx.redirectTo({
      url: '/pages/schedule/schedule'
    });
  }
});